package com.rc.evcharger.service.shop;

import com.rc.evcharger.component.UidGenerator;
import com.rc.evcharger.mapper.member.MemberAccountMapper;
import com.rc.evcharger.mapper.shop.ShopPointRecordMapper;
import com.rc.evcharger.model.member.MemberAccount;
import com.rc.evcharger.model.shop.ShopPointRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Create by Yang on 2021/5/25
 */
@Service
public class ShopPointRecordServiceImpl implements ShopPointRecordService {

    @Autowired
    private ShopPointRecordMapper shopPointRecordMapper;

    @Autowired
    private MemberAccountMapper memberAccountMapper;

    @Autowired
    private UidGenerator uidGenerator;

    @Override
    public ShopPointRecordMapper getMapper() {
        return shopPointRecordMapper;
    }

    @Override
    public void insertShopPointRecord(int point, Long memberId) {

        MemberAccount memberAccount = memberAccountMapper.selectByPrimaryKey(memberId);

        MemberAccount newMemberInfo = new MemberAccount();
        newMemberInfo.setMemberId(memberAccount.getMemberId());
        newMemberInfo.setPoint(memberAccount.getPoint() + point);
        //积分累加并保存
        memberAccountMapper.updateByPrimaryKeySelective(newMemberInfo);

        ShopPointRecord shopPointRecord = new ShopPointRecord();
        shopPointRecord.setTenantCode(memberAccount.getTenantCode());
        shopPointRecord.setPoint(point);
        shopPointRecord.setAfterPoint(newMemberInfo.getPoint());
        shopPointRecord.setMemberId(memberId);
        shopPointRecord.setLocalTradeNo(uidGenerator.genChargeOrderNum(memberId.toString()));
        shopPointRecord.setPointRecordType("1");//记录类型 0消费积分 1充电新增积分  2签到积分  3.历史累积积分
        shopPointRecord.setCreatedTime(new Date());
        shopPointRecord.setLastUpdatedTime(new Date());

        //保存积分记录
        shopPointRecordMapper.insertSelective(shopPointRecord);
    }
}
